package com.codebuffer.nowcoder;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class FirstAppearingOnce {

    Map<Character, Integer> map = new HashMap();
    Queue<Character> queue = new LinkedBlockingQueue<>();

    //Insert one char from stringstream
    public void Insert(char ch)
    {
        map.put(ch,map.getOrDefault(ch,0)+1);
        queue.add(ch);

    }
    //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        while (queue.size()>0){
            Character poll = queue.peek();
            if(map.get(poll)==1){
                return poll;
            }else {
                queue.remove();
            }
        }
        return '#';
    }
}
